package com.zzptc.shop.db;

import com.zzptc.shop.util.MyUtil;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class MysqlDBImp extends IDB{
    private Connection conn;
    private String DRIVER;
    private String URL;
    private String UNAME;
    private String PWD;



    //构造方法的结构    修饰符  + 方法名（与类名相同） +参数列表 + 抛异常
    public MysqlDBImp(){
        try {
           Map<String,String> map=MyUtil.readXML("db-config.xml");
            String useUnicode=map.get("useUnicode");
            String characterEncoding=map.get("characterEncoding");
           DRIVER=map.get("driver");
           URL=map.get("url")+"?useUnicode="+useUnicode+"&characterEncoding="+characterEncoding;
           UNAME=map.get("username");
           PWD=map.get("password");
          conn=DriverManager.getConnection(URL,UNAME,PWD);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public Connection getConn() throws SQLException {

        return conn;
    }

    @Override
    public void closeConn() throws SQLException {
        if(conn!=null&&!conn.isClosed()){
            conn.close();
        }

    }

    public static void main(String[] args) {
        MysqlDBImp mysqlDBImp=new MysqlDBImp();
        try {
            System.out.println( mysqlDBImp.getConn().isClosed());
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
